-module(test).
-export([demo/0]).

-define(DEFLATE, 1). %% - 压缩
-define(INFLATE, 2). %% - 解压

str() ->
    "this is test string".

demo() ->
    erl_ddll:load_driver(".", ejabberd_zlib_drv),
    Port = open_port({spawn, ejabberd_zlib_drv}, [binary]),
    {ok, Res1} = deflate(Port, str()),
    {ok, Res2} = inflate(Port, Res1),
    io:format("~p~n", [str()]),
    io:format("~p~n", [Res1]),
    io:format("~p~n", [Res2]).

deflate(Port, Data) ->
    case port_control(Port, ?DEFLATE, Data) of
	<<0, Out/binary>> ->
	    {ok, Out};
	<<1, Error/binary>> ->
	    {error, binary_to_list(Error)}
    end.

inflate(Port, Data) ->
    case port_control(Port, ?INFLATE, Data) of
	<<0, In/binary>> ->
	    {ok, In};
	<<1, Error/binary>> ->
	    {error, binary_to_list(Error)}
    end.
    
